package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.entity.req.publicReq.ReqPublicIds;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import com.mf.entity.dto.DtoSortOut;
import com.mf.entity.req.search.SearchReqSortOut;
import com.mf.mapper.MapperSortOut;
import com.mf.service.ServiceSortOut;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class ImplServiceSortOut extends ServiceImpl<MapperSortOut, DtoSortOut> implements ServiceSortOut {


    /**
     * 参数查询条件
     */
    @Override
    public LambdaQueryWrapper<DtoSortOut> fillParams(SearchReqSortOut params) {
        LambdaQueryWrapper<DtoSortOut> wrapper = loadQw();
        if (ObjectUtils.isNotEmpty(params.getKey())) {
            wrapper.and(w -> {
                w.like(DtoSortOut::getMaterailName, params.getKey())
                        .or()
                        .like(DtoSortOut::getWeight, params.getKey());
            });
        }
        wrapper.eq(ObjectUtils.isNotEmpty(params.getSortId()), DtoSortOut::getSortId, params.getSortId())
                .eq(ObjectUtils.isNotEmpty(params.getMaterialId()), DtoSortOut::getMaterialId, params.getMaterialId())
                .eq(ObjectUtils.isNotEmpty(params.getNextProcess()), DtoSortOut::getNextProcess,
                        params.getNextProcess())
                .eq(ObjectUtils.isNotEmpty(params.getRelevancyWork()), DtoSortOut::getRelevancyWork,
                        params.getRelevancyWork())
        ;
        return wrapper;
    }

    /**
     * 根据明细id集合更新关联id
     * sourceId  = 0 解除关联
     */
    @Override
    @Transactional
    public void updateRelevancyBySourceId(List<Integer> detailIds, Integer sourceId) {
        List<DtoSortOut> sortOutList = new ArrayList<>();

        for (Integer id : detailIds) {
            DtoSortOut dtoSortOut = new DtoSortOut();

            dtoSortOut.setRelevancyWork(sourceId);
            dtoSortOut.setId(id);

            sortOutList.add(dtoSortOut);
        }
        this.updateBatchById(sortOutList);
    }
}